package com.loan.service;
import java.util.List;

import com.loan.model.Notice;
import com.loan.model.User;

public class NoticeLogService extends BaseService<User>{
	
	/**
	 * 用户是否读过该消息
	 */ 
	public boolean isRead(int noticeId, int userId) {
		return Notice.dao.findFirst("select id from notice_log where notice_id=? and user_id=?", noticeId, userId)!=null;
	} 
	
	/**
	 * 总计几条数据没有读
	 */
	public long unReadNum(String type, User u) {
		Notice n = Notice.dao.findFirst("select count(n.id) as count from (select distinct id from notice where type=? and status='enable') n where n.id not in (select distinct notice_id from notice_log where user_id=?)", type, u.getId());
		return n.getLong("count")!=null && n.getLong("count") >0 ? n.getLong("count") : 0;
	}
	
	/**
	 * 返回未读数据
	 */
	public List<Notice> unRead(String type, User u) {
		return Notice.dao.find("select * from (select distinct id from notice where type=? and status='enable') n where n.id not in (select distinct notice_id from notice_log where user_id=?)", type, u.getId());
	}
}
